package com.jungle.mvvm.sample.ui.chat;

import android.databinding.Observable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.jungle.mvvm.base.BaseFragment;
import com.jungle.mvvm.sample.BR;
import com.jungle.mvvm.sample.R;
import com.jungle.mvvm.sample.databinding.FragmentChatKindBinding;

import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

/**
 * @ClassName ChatKindFragment
 * @Description ChatKindFragment
 * @Author Daniel
 * @Version V1.0.0
 */
public class ChatKindFragment extends BaseFragment<FragmentChatKindBinding, ChatKindViewModel> {

    private int mId;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_chat_kind;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        mId = getArguments().getInt("id", 0);
    }

    @Override
    public void initData() {
        //给RecyclerView添加Adpter，请使用自定义的Adapter继承BindingRecyclerViewAdapter，重写onBindBinding方法，里面有你要的Item对应的binding对象。
        // Adapter属于View层的东西, 不建议定义到ViewModel中绑定，以免内存泄漏
        binding.setAdapter(new BindingRecyclerViewAdapter());
        if (getUserVisibleHint()) {
            viewModel.initData(mId);
        }
    }

    public void initFragment(int mId) {
        viewModel.initData(mId);
    }

    @Override
    public void initViewObservable() {
        viewModel.uc.finishRefreshing.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable observable, int i) {
                binding.twinklingRefreshLayout.finishRefreshing();
            }
        });
        viewModel.uc.finishLoadmore.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable observable, int i) {
                binding.twinklingRefreshLayout.finishLoadmore();
            }
        });
    }

    /**
     * 创建Fragment
     *
     * @return ChatKindFragment
     */
    public static ChatKindFragment newInstance(int id) {
        ChatKindFragment fragment;
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        synchronized (ChatKindFragment.class) {
            fragment = new ChatKindFragment();
            fragment.setArguments(bundle);
        }
        return fragment;
    }

}
